package com.abaaba.project2.service.wx.wxserviceimpl;

import com.abaaba.project2.bean.AdTopic;
import com.abaaba.project2.bean.AdTopicExample;
import com.abaaba.project2.bean.Goods;
import com.abaaba.project2.bean.bo.WxTopicListBO;
import com.abaaba.project2.bean.vo.wx.WxTopicDetailVO;
import com.abaaba.project2.bean.vo.wx.WxTopicListVO;
import com.abaaba.project2.bean.vo.wx.WxTopicRelatedVO;
import com.abaaba.project2.mapper.AdTopicMapper;
import com.abaaba.project2.service.wx.WxTopicListService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.SneakyThrows;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Hedgehog
 * @Date 2021/6/8 23:51
 * @Version 1.0
 */
@Service
public class WxTopicListServiceImpl implements WxTopicListService {

    @Autowired
    AdTopicMapper adTopicMapper;
    @Override
    public WxTopicListBO searchTopic(WxTopicListVO wxTopicListVO) {
        //先创建bo以及要用的对象
        AdTopicExample adTopicExample = new AdTopicExample();
        WxTopicListBO wxTopicListBO = new WxTopicListBO();
        ArrayList<WxTopicListBO.DataBean> dataBeans = new ArrayList<>();
        //进行查询，封装到bo中，然后封装到list中
        int count=0;
        List<AdTopic> adTopics = adTopicMapper.selectByExample(adTopicExample);
        for (AdTopic adTopic : adTopics) {
            WxTopicListBO.DataBean dataBean = new
                    WxTopicListBO.DataBean(adTopic.getId(),adTopic.getTitle(),adTopic.getSubtitle(),adTopic.getId(),adTopic.getReadCount(),adTopic.getPicUrl());
            dataBeans.add(dataBean);
            count++;
        }
        wxTopicListBO.setData(dataBeans);
        wxTopicListBO.setCount(count);

        return wxTopicListBO;
    }

    @SneakyThrows
    @Override
    public ArrayList<WxTopicRelatedVO> relatedTopic(Integer id) {

        //创建返回的volist
        // 进行查询
        // 封装，ifid不等于你要的进行封装，
        ArrayList<WxTopicRelatedVO> wxTopicRelatedVOS = new ArrayList<>();

        AdTopicExample adTopicExample = new AdTopicExample();
        List<AdTopic> adTopics = adTopicMapper.selectByExample(adTopicExample);
        int count=0;
        for (AdTopic adTopic : adTopics) {
            //我们需要的只有四个数据，因此对比以下id，如果不包含查询中传入的，进行转化
            //转化之后进行封装，封装的时候看是否超过四个，不超过就插进去，然后计数器加一

            String[] strings = new String[0];
            if(!adTopic.getId().equals(id)){
                //String title = adTopic.getTitle();
                //String subtitle = adTopic.getSubtitle();
                //int price = adTopic.getPrice().intValue();
                //String readCount = adTopic.getReadCount();
                //String picUrl = adTopic.getPicUrl();
                //Integer sortOrder = adTopic.getSortOrder();
                //String addTime = adTopic.getAddTime().toString();
                //String updateTime = adTopic.getUpdateTime().toString();
                //Boolean deleted = adTopic.getDeleted();
                //String content = adTopic.getContent();

                WxTopicRelatedVO wxTopicRelatedVO = new WxTopicRelatedVO();
                //wxTopicRelatedVO.setId(id);
                //wxTopicRelatedVO.setTitle(title);
                //wxTopicRelatedVO.setSubtitle(subtitle);
                //wxTopicRelatedVO.setPrice(price);
                //wxTopicRelatedVO.setReadCount(readCount);
                //wxTopicRelatedVO.setPicUrl(picUrl);
                //wxTopicRelatedVO.setSortOrder(sortOrder);
                //wxTopicRelatedVO.setAddTime(addTime);
                //wxTopicRelatedVO.setUpdateTime(updateTime);
                //wxTopicRelatedVO.setDeleted(deleted);
                //wxTopicRelatedVO.setContent(content);
                BeanUtils.copyProperties(wxTopicRelatedVO,adTopic);
                if (count<4){
                    wxTopicRelatedVOS.add(wxTopicRelatedVO);
                    count++;
                }
            }
        }
        return wxTopicRelatedVOS;
    }

    @Override
    public WxTopicDetailVO topicDetail(Integer id) {
        WxTopicDetailVO wxTopicDetailVO = new WxTopicDetailVO();
        WxTopicDetailVO.TopicBean topicBean = new WxTopicDetailVO.TopicBean();
        AdTopic adTopic = adTopicMapper.selectByPrimaryKey(id);
        try {
            BeanUtils.copyProperties(topicBean,adTopic);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        wxTopicDetailVO.setTopic(topicBean);
        return wxTopicDetailVO;
    }


}
